﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using DLL.Entities;
using DLL;
namespace DLL
{
    public class XMLHandler
    {
        public static bool WriteXML(object savedObj, string fileToSave, XmlSerializer serializer)
        {
            try
            {
                //Create the serializer object using the type definition
                TextWriter textWriter = new StreamWriter(fileToSave);
                //Use the serializer object to write out the DS_Obj that holds the form data
                serializer.Serialize(textWriter, savedObj);
                textWriter.Close();
                return true;
            }
            catch (Exception ex)
            {
                Utility.LogFile(ex.ToString());
                return false;
            }
        }
        public static object ReadXML(string fileToOpen, XmlSerializer deSerializer)
            //you must set this method to return a type object so you can return the data
        {
            try
            {
                StreamReader sr = new StreamReader(fileToOpen);
                //here is where you set the serializer to a destination. serializer has to send the 
                //data to something so you set it to savedobj which is an object
                //you also gotta cast the serializer as type object
               return (object)deSerializer.Deserialize(sr);
                //here you gotta return the object that you just serialized 

            }
            catch
            {
                return null;
            }
        }

        }
    }

